<?php
App::uses('AppController', 'Controller');
/**
 * DisciplinesTeachers Controller
 *
 * @property DisciplinesTeacher $DisciplinesTeacher
 */
class DisciplinesTeachersController extends AppController {
    public $components = array('AjaxMultiUpload.Upload');
    /**
     * managerWeight
     * 
     * @access public
     * Manager the discipline's weight
     * @return mixed Value.
     */
    public function managerWeight()
    {
    	$this->loadModel('Teacher');
    	$this->loadModel('Discipline');
    	$teacher = $this->Teacher->find('first', array('conditions' => array('Teacher.userfk' => AuthComponent::user('id'))));
    	if($this->request->is('post')){
    		$data = array();
    		$this->DisciplinesTeacher->deleteAll(array('DisciplinesTeacher.teacher_id' => $teacher['Teacher']['id']));
    		foreach ($this->request->data as $discipline => $weight) {
    			if($weight != 0)
    			{
    				$data[] = array(
    				'DisciplinesTeacher' => array(
    					'discipline_id' => $discipline,
    					'teacher_id' => $teacher['Teacher']['id'],
    					'weight' => $weight
    					)
    				);
    			}
    			
    		}
    		if($this->DisciplinesTeacher->saveMany($data)){
    			$this->Session->setFlash(__("The preferences has been saved"));
    		}
    		else
    		{
    			$this->Session->setFlash(__("The preferences could not be saved. Please, try again."));
    		}
    	}
    	
    	$weights = 
    	$this->DisciplinesTeacher->find('list', array(
    		'conditions' => array('DisciplinesTeacher.teacher_id' => $teacher['Teacher']['id']
    			),
    		'fields' => array('DisciplinesTeacher.discipline_id', 'DisciplinesTeacher.weight')
    		));
    	$this->set('disciplines', $this->Discipline->find('all', array('order' => array('Discipline.name' => 'ASC', 'Discipline.semester' => 'ASC'))));
    	$this->set('choices', $this->DisciplinesTeacher->find('available', array('conditions' => array('DisciplinesTeacher.teacher_id !=' => $teacher['Teacher']['id'], 'DisciplinesTeacher.weight != ' => 0))));
    	$this->set('weights', $weights);

    }

    public function myChoices()
    {
        $this->loadModel('Teacher');
        $this->loadModel('Discipline');
        $teacher = $this->Teacher->find('first', array('conditions' => array('Teacher.userfk' => AuthComponent::user('id'))));
        if($this->request->is('post')){
            $data = array();
            $this->DisciplinesTeacher->deleteAll(array('DisciplinesTeacher.teacher_id' => $teacher['Teacher']['id']));
            foreach ($this->request->data as $discipline => $weight) {
                if($weight != 0)
                {
                    $data[] = array(
                    'DisciplinesTeacher' => array(
                        'discipline_id' => $discipline,
                        'teacher_id' => $teacher['Teacher']['id'],
                        'weight' => $weight
                        )
                    );
                }
                
            }
            if($this->DisciplinesTeacher->saveMany($data)){
                $this->Session->setFlash(__("The preferences has been saved"));
            }
            else
            {
                $this->Session->setFlash(__("The preferences could not be saved. Please, try again."));
            }
        }
        
        $weights = 
        $this->DisciplinesTeacher->find('list', array(
            'conditions' => array('DisciplinesTeacher.teacher_id' => $teacher['Teacher']['id']
                ),
            'fields' => array('DisciplinesTeacher.discipline_id', 'DisciplinesTeacher.weight')
            ));
        $mychoices = $this->DisciplinesTeacher->find('list', array('fields'=> array('DisciplinesTeacher.discipline_id'),'conditions' => array('DisciplinesTeacher.teacher_id =' => $teacher['Teacher']['id'])));
        $ids = implode(',', $mychoices);
        if(count($mychoices))
            $this->set('disciplines', $this->Discipline->find('all', array('order' => array('Discipline.name' => 'ASC', 'Discipline.semester' => 'ASC'), 'conditions' => array('Discipline.id IN ('.$ids.')'))));
        else
            $this->set('disciplines', null);
        $this->set('choices', $this->DisciplinesTeacher->find('available', array('conditions' => array('DisciplinesTeacher.teacher_id !=' => $teacher['Teacher']['id'], 'DisciplinesTeacher.weight != ' => 0))));
        $this->set('weights', $weights);

    }

    public function listWeights(){
        $this->loadModel('Teacher');
        $this->loadModel('Discipline');
        $choices = array();
        $teachers = $this->Teacher->find('all',array('order' => array('Teacher.name' => 'ASC')));

        foreach ($teachers as $key => $value) {
           $choices[$value['Teacher']['id']] = $this->DisciplinesTeacher->find('list', array(
            'conditions' => array('DisciplinesTeacher.teacher_id' => $value['Teacher']['id']
                ),
            'fields' => array('DisciplinesTeacher.discipline_id', 'DisciplinesTeacher.weight')
            ));
        }
        $this->set('choices', $choices);
        $this->set('teachers', $teachers);
        $this->set('disciplines', $this->Discipline->find('all', array('order' => array('Discipline.semester' => 'ASC', 'Discipline.name' => 'ASC'))));
        //$this->set('choices', $this->DisciplinesTeacher->find('available', array('conditions' => array('DisciplinesTeacher.teacher_id !=' => $teacher['Teacher']['id'], 'DisciplinesTeacher.weight != ' => 0))));
    }

    public function beforeFilter()
    {
         if (!($this->request->params['action'] == 'listWeights'))
            $this->isAuthorized("teacher");
        else
            $this->isAuthorized("admin");
    	
    }
}
